﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CCF20Model;
using WiseLink.RDS.Resource;

namespace CCF20Model.FaultHandler
{
    class ProcessAllFaultHandler2 : FaultHandlerBase
    {
        public ProcessAllFaultHandler2(IFaultHandler faultHandler)
        {
            this.ccf20Ent = faultHandler.ccf20Ent;
            this.PreviousHandler = faultHandler;
            this.MyVehicle = faultHandler.MyVehicle;
            this.MyFault = faultHandler.MyFault;
        }
        public bool IsDuplicateFault()
        {
            Fault currentFault = null;
            if (MyFault.FaultType.Contains(WiseLinkRDSStatus.Fault_Type_Fault))
            {
                //对于相同的车辆的同一个故障，视为重复
                currentFault = ccf20Ent.Faults.Where(f => f.IsActive && f.VehicleID == MyVehicle.VehicleID && f.Code == MyFault.Code).FirstOrDefault();
                if (currentFault != null)
                {
                    MyFault.IsActive = false;
                    MyFault.CloseReason = "重复故障";
                    LoggerHelper.LogZICDebug("[故障分析]对于同一车辆的有一个相同的还未处理的故障-视为重复");
                }
            }
            else if (MyFault.FaultType.Contains(WiseLinkRDSStatus.Fault_Type_Urgent))
            {
                //对于撞车,同一辆车多个撞车，只取最后一个
                currentFault = ccf20Ent.Faults.Where(f => f.IsActive && f.VehicleID == MyVehicle.VehicleID && 
                                                        f.FaultType.Contains(WiseLinkRDSStatus.Fault_Type_Urgent)
                                                     ).FirstOrDefault();
                if (currentFault != null && currentFault.HappenDate + new TimeSpan(0, 5, 0) > MyFault.HappenDate)
                {
                    MyFault.IsActive = false;
                    MyFault.CloseReason = "重复撞车";
                    LoggerHelper.LogZICDebug("[故障分析]对于同一车辆有多于一个的还未处理的撞车-视为重复");
                }
                else
                {
                    //如果求救超过5分钟，则不屏蔽
                    LoggerHelper.LogZICDebug("[故障分析]如果求救超过5分钟，不屏蔽");
                    currentFault = null;
                }
            }
            else if (MyFault.FaultType.Contains(WiseLinkRDSStatus.Fault_Type_Maintenance))
            {
                //对于撞车和里程，对于相同的车辆,相同的里程，视为重复
                currentFault = ccf20Ent.Faults.Where(f => f.IsActive && f.VehicleID == MyVehicle.VehicleID && f.CurrentMileage == MyFault.CurrentMileage).FirstOrDefault();
                if (currentFault != null)
                {
                    MyFault.IsActive = false;
                    MyFault.CloseReason = "重复里程";
                    LoggerHelper.LogZICDebug("[故障分析]对于同一车辆有多于一个相同的里程-视为重复");

                }
            }
            if (currentFault != null)
            {
                //如果是重复的故障码，我们要跟新现有的FAULTID，避免重复取之
                // 无论是否重复故障，我们都写入FAULT表
                if (currentFault.AnchiID < MyFault.AnchiID)
                {
                    currentFault.AnchiID = MyFault.AnchiID;
                    currentFault.HappenDate = MyFault.HappenDate;
                }
                return true; //if we get the MyFault already, then do nothing.
            }
            else
            {
                return false;
            }

        }
        public override void ProcessRequest()
        {
            IFaultHandler nextHandler = null;
            if (IsDuplicateFault())
            {
                CloseFault("重复故障");
                nextHandler = new CompleteFaultHandler(this);
            }
            else
            {
                if (MyFault.FaultType.Contains(WiseLinkRDSStatus.Fault_Type_Fault))
                {
                    nextHandler = new ProcessFaultHandler2(this);

                }else if (MyFault.FaultType.Contains(WiseLinkRDSStatus.Fault_Type_Urgent))
                {
                    nextHandler = new ProcessUrgentHandler2(this);

                }else if (MyFault.FaultType.Contains(WiseLinkRDSStatus.Fault_Type_Maintenance))
                {
                    nextHandler = new CompleteFaultHandler(this);
                }else
                {
                    nextHandler = new CompleteFaultHandler(this);
                }
                 
            }
            nextHandler.ProcessRequest();
        }
    }

  
    class ProcessFaultHandler2 : FaultHandlerBase
    {
        public ProcessFaultHandler2(IFaultHandler faultHandler)
        {
            this.ccf20Ent = faultHandler.ccf20Ent;
            this.PreviousHandler = faultHandler;
            this.MyVehicle = faultHandler.MyVehicle;
            this.MyFault = faultHandler.MyFault;
        }
        public bool CheckPriorityAndCustomizedCode()
        {

            if (MyFault.Priority > 2)
            {
                //Low Priority Bug 
                MyFault.CloseDate = DateTime.Now;
                MyFault.IsNormalFault = true;
                MyFault.CloseReason = FaultCloseReason.Cleared;
            }
            else
            {
                MyFault.CloseReason = FaultCloseReason.Confirmed;
                MyFault.IsNormalFault = false;
            }
            
            MyFault.IsConfirmed = true;
            return true;
        }
        public override void ProcessRequest()
        {
            IFaultHandler nextHandler = null;
            if (CheckPriorityAndCustomizedCode())
            {
                MyVehicle.IsFault = true;
                LoggerHelper.LogZICDebug("[故障分析]需要处理的故障");
                MyVehicle.IsActive = true;
                MyVehicle.LatestActiveTime = DateTime.Now;
                MyVehicle.LastContactResult = MyVehicle.LastContactResult ?? WiseLink.RDS.Resource.WiseLinkRDSStatus.Contact_NotContactYet;
            }
            else
            {
                MyVehicle.IsFault = false;
                LoggerHelper.LogZICDebug("[故障分析]不需要处理的故障");
             }
            nextHandler = new CompleteFaultHandler(this);
            nextHandler.ProcessRequest();

        }
    }
}
